﻿using System;
using System.ComponentModel;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Input;
using Lib.Helper;
using MSIACCSL2.Command;
using MSIACCSL2.UserServices;
using MSIACCSL2.Views.User;
using WcfService1.DTO;

namespace MSIACCSL2.ViewModels
{
    public class LoginViewModel : ViewModelBase
    {
        private readonly MainViewModel MainVM;
        private readonly IsolatedStorageSettings UserSettings;
        private readonly UserServicesClient client;
        private bool _IsBusy;
        private string _message;
        private bool _result;
        private UserDTO _user;
        private Visibility _visible;
        private Login frmLogin;

        public LoginViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                client = new UserServicesClient();
                User = new UserDTO();
                ValidateCommand = new DelegateCommand(validateLogin, CanValidate);
                MainVM = new MainViewModel();
                UserSettings = IsolatedStorageSettings.ApplicationSettings;
                Visible = Visibility.Collapsed;
            }
        }

        public UserDTO User
        {
            get { return _user; }
            set
            {
                _user = value;
                FirePropertyChanged("User");
            }
        }

        public bool Result
        {
            get { return _result; }
            set
            {
                _result = value;
                FirePropertyChanged("Result");
                if (value)
                {
                    //MessageBox.Show("Login Diterima, Selamat bekerja");
                    //Navigation.MyNavigation.Navigate(new Uri("/Home", UriKind.Relative));
                }
                else
                {
                    Visible = Visibility.Visible;
                    MessageBox.Show("Login Gagal, Hubungi Administrator!");
                }
            }
        }

        public string Message
        {
            get { return _message; }
            set
            {
                _message = value;
                FirePropertyChanged("Message");
            }
        }

        public Visibility Visible
        {
            get { return _visible; }
            set
            {
                _visible = value;
                FirePropertyChanged("Visible");
            }
        }

        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                FirePropertyChanged("IsBusy");
            }
        }

        public ICommand ValidateCommand { get; set; }

        public bool CanValidate(Object param)
        {
            return true;
        }

        public void validateLogin(Object param)
        {
            IsBusy = true;
            client.ValidateUserCompleted += client_ValidateUserCompleted;
            client.ValidateUserAsync(User);

            
        }

        private void client_ValidateUserCompleted(object sender, ValidateUserCompletedEventArgs e)
        {
            client.ValidateUserCompleted -= client_ValidateUserCompleted;
            
            if (e.Error != null || ! string.IsNullOrEmpty(e.errorMessage))
            {
                MessageBox.Show(e.errorMessage);
                return;
            }
              

            Result = e.Result;
            IsBusy = false;
            if (Result)
            {
                User = e.result1;
                if (! UserSettings.Contains(e.SessionID))
                    //Save User to Local Data
                    UserSettings.Add(e.SessionID, User);
                //Keep User in Memory
                GlobalVar.User = User;
                if (User.Department == "Veneer")
                    GlobalVar.Dep = string.Empty;
                else
                    GlobalVar.Dep = User.Department[0].ToString();
                EventAggregator.PublishMessage("LoginSukes", User,null);
            }
        }

        public void Exit()
        {
            App.Current.MainWindow.Close();
        }
    }
}